package com.example.sunmiv1.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.PurchaseRefundDetailsAdapter;
import com.example.sunmiv1.entity.StoreRetreatOrderDetailsBean;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PurchaseRefundDetailsActivity extends Activity {
    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.rv_pur_refund)
    RecyclerView rvPurRefund;

    private static final int GET_ERROR_STATUS=0;
    private static final int INFO_GET=1;

    private String orderId;// 退貨單id
    private List<StoreRetreatOrderDetailsBean.DataBean> dataBeanList;

    private PurchaseRefundDetailsAdapter mAdapter;
    private LoadingDialog loadingDialog;

    private ACache aCache;// 緩存
    private String userType;// 用戶身份

    @SuppressLint("HandlerLeak")
    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GET_ERROR_STATUS:
                    Toast.makeText(PurchaseRefundDetailsActivity.this,"數據加載不出來~",Toast.LENGTH_SHORT).show();
                    break;
                case INFO_GET:
                    mAdapter.notifyDataSetChanged();
                    break;

            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_purchase_refund_details);
        ButterKnife.bind(this);
        titleTitle.setText(R.string.purchaseRefundDetailTitle);
        aCache = ACache.get(this);
        userType=aCache.getAsString("userType");
        loadingDialog=new LoadingDialog(this);
        Intent intent=getIntent();
        orderId=intent.getStringExtra("orderId");
        initData();
        initView();
    }

    private void initData() {
        dataBeanList=new ArrayList<>();
        getOrderList();
    }

    private void initView() {
        mAdapter=new PurchaseRefundDetailsAdapter(this,dataBeanList);
        rvPurRefund.setLayoutManager(new LinearLayoutManager(this));
        rvPurRefund.setAdapter(mAdapter);
    }

    private void getOrderList() {
        String url="";
        if(userType.equals("1")){
            url = Common.DETAILS_STORE_RETREAT_1;
        }else if(userType.equals("2")){
            url = Common.DETAILS_STORE_RETREAT_2;
        }
        loadingDialog.show();
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("id",orderId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        HttpUtils.post(this, url, jsonObject, new TextCallBack() {
            @Override
            protected void onSuccess(String text) {

                StoreRetreatOrderDetailsBean mStoreRetreatOrderDetailsBean= GsonUtils.JsonToBean(text,StoreRetreatOrderDetailsBean.class);
                if(mStoreRetreatOrderDetailsBean.getStatus()==1){
                    dataBeanList.addAll(mStoreRetreatOrderDetailsBean.getData());
                        mHandler.sendEmptyMessage(INFO_GET);
                }
                else
                    mHandler.sendEmptyMessage(GET_ERROR_STATUS);
                loadingDialog.dismiss();
            }

            @Override
            protected void onFailure(ResponseException e) {
                loadingDialog.dismiss();
            }
        });
    }

    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }
}
